package com.example.ymm.modules1.service.impl.hl.ex;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.exception.YmException;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.hl.ex.*;
import com.example.ymm.modules1.mapper.ex.ExPaperMapper;
import com.example.ymm.modules1.query.hl.ex.*;
import com.example.ymm.modules1.service.hl.ex.*;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
//@Transactional
@Slf4j
@DS("slave2")
public class ExPaperServiceImpl extends BaseServiceImpl<ExPaperMapper, ExPaper> implements ExPaperService {
    @Autowired
    ExPublishPaperService exPublishPaperService;
    @Autowired
    ExPaperGroupService exPaperGroupService;
    @Autowired
    ExPaperGroupQuestionService exPaperGroupQuestionService;
    @Autowired
    private ExQuestionService exQuestionService;
    @Autowired
    private ExPublishGroupService exPublishGroupService;

    @Override
    public TableList<ExPaper> list(ExPaperQueryCriteria criteria, YmPageDto pageDto) {
        IPage<ExPaper> page = new Page<ExPaper>(pageDto.getPage(),pageDto.getPageSize());
        QueryWrapper predicate = QueryHelpPlus.getPredicate(ExPaper.class, criteria);
//        predicate.orderByDesc("create_time");
        baseMapper.selectPage(page, predicate);
        TableList<ExPaper> tableList=new TableList(page);
        return tableList;
    }

    public List<ExPaper> queryAll(ExPaperQueryCriteria criteria) {
        QueryWrapper predicate = QueryHelpPlus.getPredicate(ExPaper.class, criteria);
//        predicate.orderByDesc("create_time");
        List<ExPaper> result =baseMapper.selectList(predicate);
        return result;
    }

    @Override
    public void paperSave(ExPaper exPaper) {
        boolean isAdd=false;
        if(UtilValidate.isEmpty(exPaper.getPaperId())){
            isAdd=true;
            exPaper.setStatus(0);
        }
        saveOrUpdate(exPaper);
        if(!isAdd){
            //修改时需要先清空数据
            LambdaQueryWrapper<ExPaperGroup> exPaperGroupQuery = new LambdaQueryWrapper<>();
            exPaperGroupQuery.eq(ExPaperGroup::getPaperId,exPaper.getPaperId());
            exPaperGroupService.remove(exPaperGroupQuery);

            LambdaQueryWrapper<ExPaperGroupQuestion> exPaperGroupQuestionQuery = new LambdaQueryWrapper<>();
            exPaperGroupQuestionQuery.eq(ExPaperGroupQuestion::getPaperId,exPaper.getPaperId());
            exPaperGroupQuestionService.remove(exPaperGroupQuestionQuery);
        }
        List<ExPaperGroup> groups = exPaper.getGroups();
        for (int i = 0; i < groups.size(); i++) {
            ExPaperGroup group = groups.get(i);
            group.setSort(i+1);
            group.setPaperId(exPaper.getPaperId());
            exPaperGroupService.saveOrUpdate(group);

            //同步修改已发布试卷模块
            ExPublishGroupQueryCriteria c3=new ExPublishGroupQueryCriteria();
            c3.setPaperGroupId(group.getPaperGroupId());
            List<ExPublishGroup> exPublishGroups = exPublishGroupService.queryAll(c3);
            for (ExPublishGroup exPublishGroup : exPublishGroups) {
                exPublishGroup.setLimitTime(exPublishGroup.getLimitTime());
                exPublishGroup.setTrueType0(group.getTrueType0());
                exPublishGroupService.saveOrUpdate(exPublishGroup);
            }
            //判断  选题类型：0全部 1部分
            if(group.getType()==1){
                //部分

                List<ExPaperGroupQuestion> partQuestions = group.getPartQuestions();
                for (int j = 0; j < partQuestions.size(); j++) {
                    ExPaperGroupQuestion question = partQuestions.get(j);
                    question.setSort(j+1);
                    question.setPaperId(exPaper.getPaperId());
                    question.setPaperGroupId(group.getPaperGroupId());
                }
                exPaperGroupQuestionService.saveOrUpdateBatch(partQuestions);
            }else{
                //全部 发布时再去查询题库

            }

        }
    }


    @Override
    public ExPaper paperDetail(ExPaper exPaper) {
        ExPaper paper = getById(exPaper.getPaperId());
        if(UtilValidate.isEmpty(paper)){
            throw new YmException("参数有误!");
        }
        ExPaperGroupQueryCriteria c1=new ExPaperGroupQueryCriteria();
        c1.setPaperId(paper.getPaperId());
        List<ExPaperGroup> exPaperGroups = exPaperGroupService.queryAll(c1);

        ExPaperGroupQuestionQueryCriteria c2=new ExPaperGroupQuestionQueryCriteria();
        c2.setPaperId(paper.getPaperId());
        List<ExPaperGroupQuestion> exPaperGroupQuestions = exPaperGroupQuestionService.queryAll(c2);
        for (ExPaperGroup group : exPaperGroups) {
            List<ExPaperGroupQuestion> questions = new ArrayList<>();
            for (ExPaperGroupQuestion exPaperGroupQuestion : exPaperGroupQuestions) {
                if(exPaperGroupQuestion.getPaperGroupId().equals(group.getPaperGroupId())){
                    questions.add(exPaperGroupQuestion);
                }
            }
            group.setPartQuestions(questions);
        }
        paper.setGroups(exPaperGroups);
        return paper;
    }


    @Override
    public ExPaper getByPublishPaperId(String publishPaperId) {
        ExPublishPaper publishPaper = exPublishPaperService.getById(publishPaperId);
        if(UtilValidate.isEmpty(publishPaper)){
            throw new YmException("查找不到publishPaperId");
        }
        ExPaper exPaper = getById(publishPaper.getPaperId());
        return exPaper;
    }
}


